/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-06-15 13:40:44
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-08-04 11:26:39
 * @FilePath: \ems_neutral_app\lib\common\widgets\cubitWidget\BottomSheetView.dart
 * @Description: 底部sheet
 */

import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/cupertino.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

class BottomSheetView extends StatefulWidget {
  final double? height;
  final List<String> titles;

  /// 选择第几个，默认选中第一个
  final int? initialSelectedItem;
  final Widget? itemWidget;
  final double? itemHeight;

  /// 取消
  final void Function()? tapCancelFunc;

  /// 确定，返回选中了第几个
  final void Function(int selected)? tapSureSeletedFunc;
  const BottomSheetView(
      {required this.titles,
      this.height,
      this.initialSelectedItem,
      this.itemWidget,
      this.itemHeight,
      this.tapCancelFunc,
      this.tapSureSeletedFunc,
      super.key});

  @override
  State<BottomSheetView> createState() => _BottomSheetViewState();
}

class _BottomSheetViewState extends State<BottomSheetView> {
  late int _curSeleted;
  late double _itemHeight;
  late FixedExtentScrollController _scrollController;

  @override
  void initState() {
    super.initState();

    _itemHeight = widget.itemHeight ?? 48.w;

    _curSeleted = widget.initialSelectedItem ?? 0;

    _scrollController = FixedExtentScrollController(initialItem: _curSeleted);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: ColorUtils.color_bottom_sheet,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.w), topRight: Radius.circular(12.w))),
      child: Column(
        children: [
          /// 顶部操作按钮
          Container(
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            child: handlerBtns(),
            height: 50.w,
          ),

          /// 中间可选的内容
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            height: widget.height ?? 230.w,
            child: contentView(),
          )
        ],
      ),
    );
  }

  handlerBtns() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SilButton(
          padding: EdgeInsets.all(5.w),
          title: LangOf(context)?.common?.cancel,
          titleColor: ColorUtils.color_subtitle,
          fontSize: 16.sp,
          fontWeight: FontWeight.w500,
          onTapFunc: () => widget.tapCancelFunc ?? ARoute.pop(context),
        ),
        SilButton(
          padding: EdgeInsets.all(5.w),
          title: LangOf(context)?.common?.confirm,
          titleColor: ColorUtils.color_theme,
          fontSize: 16.sp,
          fontWeight: FontWeight.w500,
          onTapFunc: () {
            if (widget.tapSureSeletedFunc != null) {
              widget.tapSureSeletedFunc!(_curSeleted);
            }
            ARoute.pop(context);
          },
        ),
      ],
    );
  }

  contentView() {
    return CupertinoPicker.builder(
      childCount: widget.titles.length,
      scrollController: _scrollController,
      itemExtent: _itemHeight,
      onSelectedItemChanged: (index) {
        _curSeleted = index;
      },
      itemBuilder: (context, index) {
        return widget.itemWidget ??
            Container(
              alignment: Alignment.center,
              child: SilText(
                widget.titles[index],
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: ColorUtils.color_font_main,
              ),
            );
      },
      // selectionOverlay: selectionOverlayWidget()
    );
  }

  /// 覆盖在中间的组件
  Widget selectionOverlayWidget() {
    Widget promptWidget = Container(
      height: _itemHeight - 1.w,
      alignment: Alignment.center,
      child: Padding(
        padding: EdgeInsets.only(left: 80),
        child: SilText(
          '',
          fontSize: 18.sp,
        ),
      ),
    );

    // 分割线
    Widget dividerWidget = Container(
      color: Color.fromRGBO(0, 0, 0, 0.1),
      height: 0.5.w,
    );

    return Column(
      children: [dividerWidget, promptWidget, dividerWidget],
    );
  }
}
